package Control;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import Database.TableOperations;
import Model.LoyaltyCustomer;

public class CardView extends JFrame implements ActionListener
{
        private JLabel cID,cName,cAddress,cPhone,cEmail,cStart,cPoints;
        private JTextField id,name,address,phone,email,start,points;
        private JPanel upper,lower;
        private JButton next,back,done,first;
        private TableOperations to;
        private SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy");
        private  ArrayList<LoyaltyCustomer> lc;
        private int counter;
       
        public CardView(TableOperations to)
        {
                counter = 0;
               
                setTitle("MDL - Loyalty Customers");
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                setLocationRelativeTo(null);
                setSize(275, 325);
                setLayout(new GridBagLayout());
                GridBagConstraints gc = new GridBagConstraints();
                
                this.to = to;
                lc = to.returnCList();
           
                
                upper = new JPanel();
                lower = new JPanel();
               
                add(upper);
                add(lower);
               
                //panel 1
                upper.setLayout(new GridLayout(7,2));
                upper.setBorder(new TitledBorder("Customer Details"));
                cID = new JLabel("Card ID");
                id = new JTextField(10);
                cName = new JLabel("Name");
                name = new JTextField(10);
                cAddress = new JLabel("Address");
                address = new JTextField(10);
                cPhone = new JLabel("Phone");
                phone = new JTextField(10);
                cEmail = new JLabel("Email");
                email = new JTextField(10);
                cStart = new JLabel("Start Date");
                start = new JTextField(10);
                cPoints = new JLabel("Points");
                points = new JTextField(10);            
               
                upper.add(cID);
                upper.add(id);
                upper.add(cName);
                upper.add(name);
                upper.add(cAddress);
                upper.add(address);
                upper.add(cPhone);
                upper.add(phone);
                upper.add(cEmail);
                upper.add(email);
                upper.add(cStart);
                upper.add(start);
                upper.add(cPoints);
                upper.add(points);
               
                //panel 2
                lower.setBorder(new TitledBorder("Options"));
                lower.setLayout(new GridLayout(2,2));
                next = new JButton(">");
                next.addActionListener(this);
                back = new JButton("<");
                back.addActionListener(this);
                first = new JButton("First");
                first.addActionListener(this);
                done = new JButton("Done");
                done.addActionListener(this);
                lower.add(back);
                lower.add(next);
                lower.add(first);
                lower.add(done);
               
               
                        gc.gridx = 0;
                        gc.gridy = 1;
                        gc.fill = GridBagConstraints.BOTH;
                        add(lower, gc);
                        
                setFirst();            
        }

       
           public void setFirst()
       {                                
                        counter = 0;
                        id.setText(Integer.toString(lc.get(counter).getCustID()));
                        name.setText(lc.get(counter).getName());
                        address.setText(lc.get(counter).getAddress());
                        phone.setText(lc.get(counter).getPhone());
                        email.setText(lc.get(counter).getEmail());
                        points.setText(Integer.toString(lc.get(counter).getPoints()));
                        String date = format.format(lc.get(counter).getInDate());
                        start.setText(date);
       }
           
        @Override
        public void actionPerformed(ActionEvent e)
        {
                if(e.getSource() == done)
                {
                        this.dispose();
                }
                else if(e.getSource() == first)
                {
                        setFirst();
                }
                else if(e.getSource() == next)
                {
                        counter++;
                        if(counter >= lc.size())
                                setFirst();                    
                        else
                        {
                                System.out.println(counter);                    
                                id.setText(Integer.toString(lc.get(counter).getCustID()));
                                name.setText(lc.get(counter).getName());
                                address.setText(lc.get(counter).getAddress());
                                phone.setText(lc.get(counter).getPhone());
                                email.setText(lc.get(counter).getEmail());
                                points.setText(Integer.toString(lc.get(counter).getPoints()));
                                String date = format.format(lc.get(counter).getInDate());
                                start.setText(date);
                        }              
                }
                else if(e.getSource() == back)
                {
                        counter--;
                        if(counter < 0)
                                counter = (lc.size() - 1);              
                        id.setText(Integer.toString(lc.get(counter).getCustID()));
                        name.setText(lc.get(counter).getName());
                        address.setText(lc.get(counter).getAddress());
                        phone.setText(lc.get(counter).getPhone());
                        email.setText(lc.get(counter).getEmail());
                        points.setText(Integer.toString(lc.get(counter).getPoints()));
                        String date = format.format(lc.get(counter).getInDate());
                        start.setText(date);
                }
        }
}